using System;
using System.ComponentModel.Composition;
using System.Windows.Input;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Silverlight.Weblog.Client.CoreBL.Widgets;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Client.DAL.ViewModel;
using Silverlight.Weblog.Common.IoC;
using Silverlight.Weblog.Server.DAL;

namespace Silverlight.Weblog.Client.Default.Widgets
{
    public class LinksSidebarWidgetViewModel : ViewModelBase, ILinksSidebarWidgetViewModel, IDisposable
    {
        public LinksSidebarWidgetViewModel()
        {
            IoC.Get<IEventAggregator>().GetEvent<InitiaPayloadLoadedEvent>().Subscribe(UpdateViewModel);

            RaiseGoToBlogPostPermlinkEvent = new DelegateCommand<string>(
                (s) => IoC.Get<IEventAggregator>().GetEvent<GoToBlogPostPermlinkEvent>().Publish(s));
            RaiseNavigateToHomePageEvent = new DelegateCommand<User>(
                (u) => IoC.Get<IEventAggregator>().GetEvent<NavigateToHomePageEvent>().Publish(u));
        }

        public void UpdateViewModel(User obj)
        {
            Model = obj;

            UpdateVisualState();
        }

        private void UpdateVisualState()
        {
            if (string.IsNullOrEmpty(Model.AboutPermlink))
                GoToState("HideAbout");
            else
                GoToState("ShowAbout");
        }

        #region Model
        private User _model;
        public User Model
        {
            get { return _model; }
            set
            {
                if (_model != value)
                {
                    _model = value;
                    RaisePropertyChanged("Model");
                    RaisePropertyChanged((LinksSidebarWidgetViewModel vm) => vm.AboutBlogPostPermlink);
                    RaisePropertyChanged((LinksSidebarWidgetViewModel vm) => vm.RSS);
                    RaisePropertyChanged((LinksSidebarWidgetViewModel vm) => vm.Atom);
                }

            }
        }
        #endregion Model

  
        public Uri RSS
        {
            get
            {
                if (Model == null)
                    return null;

                return new Uri(string.Format("{0}/Feed/rss/{1}", Model.HomeUrl, Model.Username), UriKind.RelativeOrAbsolute);
            }
        }

        public Uri Atom
        {
            get
            {
                if (Model == null)
                    return null;

                return new Uri(string.Format("{0}/Feed/atom/{1}", Model.HomeUrl, Model.Username), UriKind.RelativeOrAbsolute);
            }
        }

        public string AboutBlogPostPermlink
        {
            get
            {
                if (Model == null)
                    return null;

                return Model.AboutPermlink;
            }
        }

        #region Implementation of IDisposable

        public void Dispose()
        {
            IoC.Get<IEventAggregator>().GetEvent<InitiaPayloadLoadedEvent>().Unsubscribe(UpdateViewModel);
        }

        #endregion

        public ICommand RaiseGoToBlogPostPermlinkEvent { get; set; }
        public ICommand RaiseNavigateToHomePageEvent { get; set; }
    }
}